package org.jbpm.pvm.internal.cmd;

import org.jbpm.api.NewDeployment;
import org.jbpm.api.cmd.Command;
import org.jbpm.api.cmd.Environment;
import org.jbpm.internal.log.Log;
import org.jbpm.pvm.internal.session.DbSession;

public class UpdateDeploymentParentCmd implements Command<Object> {
	private static final long serialVersionUID = 1L;

	private static final Log log = Log.getLog(UpdateDeploymentParentCmd.class
			.getName());

	String deploymentId;

	Long pdbid;

	public UpdateDeploymentParentCmd(String deploymentId, Long pdbid) {
		this.deploymentId = deploymentId;
		this.pdbid = pdbid;
	}

	public Object execute(Environment environment) throws Exception {
		DbSession dbSession = environment.get(DbSession.class);
		NewDeployment deployment = (NewDeployment) dbSession
				.createDeploymentQuery().deploymentId(deploymentId)
				.uniqueResult();
		if (pdbid != null) {
			NewDeployment pd = (NewDeployment) dbSession
					.createDeploymentQuery().deploymentId(Long.toString(pdbid))
					.uniqueResult();
			deployment.setParentDeployment(pd);
		} else {
			deployment.setParentDeployment(null);
		}
		log.debug("updating deployment " + deploymentId);
		dbSession.update(deployment);

		return null;

	}
}
